#include "ball.h"

#include <stdio.h>

//----------------------------------------------------------------------

Ball::Ball( Screen* s ) {
	x = 40;
	y = 10;
	direction = UR;
	screen = s;
	screen->setBall( x, y );
}

//----------------------------------------------------------------------
		
void Ball::setDirection( Direction d ) {
	direction = d;
}
		
//----------------------------------------------------------------------
		
void Ball::update() {
	if( x == 2 ) {
		if( screen->get( x-2, y ) == '|' || screen->get( x-2, y-1 ) == '|' || screen->get( x-2, y+1 ) == '|' ) {
			if( direction == UL ) direction = UR;
			else if( direction == DL ) direction = DR;
		} else {
			throw "Koniec gry. Wygral gracz prawy";
		}
	} else if ( x == 78 ) {
		if( screen->get( x+1, y ) == '|' || screen->get( x+1, y-1 ) == '|' || screen->get( x+1, y+1 ) == '|' ) {
			if( direction == UR ) direction = UL;
			else if( direction == DR ) direction = DL;
		} else {
			throw "Koniec gry. Wygral grasz lewy";
		}
	}
	
	if( y == 0 ) {
		if( direction == UR ) direction = DR;
		else if( direction == UL ) direction = DL;
	} else if ( y == 23 ) {
		if( direction == DR ) direction = UR;
		else if( direction == DL ) direction = UL;
	}	
	
	if( direction == UR ) {
		y--;
		x++; x++;
	} else if( direction == DR ) {
		y++;
		x++; x++;
	} else if( direction == DL ) {
		y++;
		x--; x--;
	} else if( direction == UL ) {
		y--;
		x--; x--;
	}
	
	if( x < 0 ) x = 0;
	else if( x > 80 ) x = 80;
	
	if( y < 0 ) y = 0;
	else if( y > 24 ) y = 24;
	
	screen->setBall( x, y );
}

//----------------------------------------------------------------------
